package com.hh.base.util.ext

import android.annotation.SuppressLint
import android.os.Build
import androidx.annotation.RequiresApi
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.YearMonth
import java.util.Date

/**
 * Create By hHui on 2025/4/3 16:58
 *
 * @description 时间类型扩展
 */
enum class DateType {
    YEAR,
    MONTH,
    DAY,
    HOUR,
    MINUTE,
    SECOND,
    H_M,
    H_M_S
}

//时间转字符串显示 默认转换到秒
@SuppressLint("SimpleDateFormat")
fun Date.toShow(type: DateType = DateType.SECOND): String {
    return when (type) {
        DateType.YEAR -> SimpleDateFormat("yyyy").format(this)
        DateType.MONTH -> SimpleDateFormat("yyyy-MM").format(this)
        DateType.DAY -> SimpleDateFormat("yyyy-MM-dd").format(this)
        DateType.HOUR -> SimpleDateFormat("yyyy-MM-dd HH").format(this)
        DateType.MINUTE -> SimpleDateFormat("yyyy-MM-dd HH:mm").format(this)
        DateType.SECOND -> SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this)
        DateType.H_M_S -> SimpleDateFormat("HH:mm:ss").format(this)
        DateType.H_M -> SimpleDateFormat("HH:mm").format(this)
    }
}

//自定义格式显示
@SuppressLint("SimpleDateFormat")
fun Date.toCustomShow(format: String): String {
    return SimpleDateFormat(format).format(this)
}

@SuppressLint("SimpleDateFormat")
fun Date.toFileName(): String {
    return SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(this)
}

//获取月份剩余日期（包括当天）
@RequiresApi(Build.VERSION_CODES.O)
fun Date.getRemainingDaysInMonth(): List<String> {
    val datePart = this.toShow(DateType.DAY)
    val date = LocalDate.parse(datePart)
    val yearMonth = YearMonth.from(date)
    val result = mutableListOf<String>()
    var currentDate = date
    while (YearMonth.from(currentDate) == yearMonth) {
        result.add(currentDate.toString())
        currentDate = currentDate.plusDays(1)
    }
    return result
}